import { FormItemRule } from "naive-ui";
import { useUserStore } from '@/store/modules/user'
import { storeToRefs } from "pinia";
const userStore = useUserStore()
const { info } = storeToRefs(userStore)
/**
 * 增加表单配置
 */
export const addFormConfig: Record<string, any> = {
    cols: 24,
    xGap: 24,
    labelPlacement: 'left',
    labelWidth: 120,
    formItems: [
        {
            field: "user_id",
            labelName: "用户id",
            labelStyle: "fontSize:14px;",
            type: "Input",
            inputType: "text",
            inputWidth: "180px",
            placeholder: "请填写用户id",
            span: 12
        },
        {
            field: "password",
            labelName: "密码",
            labelStyle: "fontSize:14px;",
            type: "Input",
            inputType: "text",
            inputWidth: "180px",
            placeholder: "请填写密码",
            span: 12
        },
        {
            field: "email",
            labelName: "邮箱",
            labelStyle: "fontSize:14px;",
            type: "Input",
            inputType: "email",
            inputWidth: "180px",
            placeholder: '请填写邮箱',
            span: 12
        },
        {
            field: "sex",
            labelName: "性别",
            labelStyle: "fontSize:14px;",
            type: "Select",
            placeholder: "-请选择性别-",
            selectWidth: "180px",
            span: 12,
            options: [
                {
                    label: '男',
                    value: '男'
                },
                {
                    label: '女',
                    value: '女'
                },
            ],
        },
        {
            field: "is_admin",
            labelName: "是否为管理员",
            labelStyle: "fontSize:14px;",
            type: "RadioBox",
            name: "radioBox",
            span: 12,
            options: [
                {
                    label: "是",
                    value: "是",
                },
                {
                    label: "否",
                    value: "否",
                },
            ],
        },

        {
            field: "signature",
            labelName: "个性签名",
            labelStyle: "fontSize:14px;",
            type: "TextArea",
            inputType: "textarea",
            placeholder: '请输入个性签名',
            autosize: {
                minRows: 3,
                maxRows: 5,
            },
            span: 24
        },


    ],
    rules: {
        user_id: {
            required: true,
            trigger: ["input"],
            message: "请填写用户id",
            validator(rule: FormItemRule, value: string) {
                if (value.length === 0) {
                    return new Error("未填写用户id");
                }
                return true;
            },
        },
        password: {
            required: true,
            trigger: ["input"],
            message: "请填写密码",
            validator(rule: FormItemRule, value: string) {
                if (value.length === 0) {
                    return new Error("未填写密码");
                }
                return true;
            },
        },
        email: {
            required: true,
            trigger: ["input"],
            message: "请填写邮箱",
            validator(rule: FormItemRule, value: string) {
                const emailReg = /^\w+@[a-z0-9]+(\.[a-z]+){1,3}$/;
                
                if (!emailReg.test(value.trim())) {
                    
					return new Error("邮箱格式错误");
				}
				return true;
			},
        },
        is_admin: {
            required: true,
            trigger: ["input"],
            message: "请选择是否为管理员",
            validator(rule: FormItemRule, value: string) {
                if (value.length === 0) {
                    return new Error("未选择");
                }
                return true;
            },
        },

    },
};


/**
 * 修改表单配置
 */
export const updateFormConfig: Record<string, any> = {
    cols: 24,
    xGap: 24,
    labelPlacement: 'left',
    labelWidth: 120,
    formItems: [
        {
            field: "user_id",
            labelName: "用户id",
            labelStyle: "fontSize:14px;",
            type: "Input",
            inputType: "text",
            inputWidth: "180px",
            placeholder: "请填写用户id",
            span: 12
        },
        {
            field: "password",
            labelName: "密码",
            labelStyle: "fontSize:14px;",
            type: "Input",
            inputType: "text",
            inputWidth: "180px",
            placeholder: "请填写密码",
            span: 12
        },
        {
            field: "email",
            labelName: "邮箱",
            labelStyle: "fontSize:14px;",
            type: "Input",
            inputType: "email",
            inputWidth: "180px",
            placeholder: '请填写邮箱',
            span: 12
        },
        {
            field: "sex",
            labelName: "性别",
            labelStyle: "fontSize:14px;",
            type: "Select",
            placeholder: "-请选择性别-",
            selectWidth: "180px",
            span: 12,
            options: [
                {
                    label: '男',
                    value: '男'
                },
                {
                    label: '女',
                    value: '女'
                },
            ],
        },
        {
            field: "is_admin",
            labelName: "是否为管理员",
            labelStyle: "fontSize:14px;",
            type: "RadioBox",
            name: "radioBox",
            span: 12,
            options: [
                {
                    label: "是",
                    value: "是",
                },
                {
                    label: "否",
                    value: "否",
                },
            ],
        },

        {
            field: "signature",
            labelName: "个性签名",
            labelStyle: "fontSize:14px;",
            type: "TextArea",
            inputType: "textarea",
            placeholder: '请输入个性签名',
            autosize: {
                minRows: 3,
                maxRows: 5,
            },
            span: 24
        },


    ],
    rules: {
        user_id: {
            required: true,
            trigger: ["input"],
            message: "请填写用户id",
            validator(rule: FormItemRule, value: string) {
                if (value.length === 0) {
                    return new Error("未填写用户id");
                }
                return true;
            },
        },
        password: {
            required: true,
            trigger: ["input"],
            message: "请填写密码",
            validator(rule: FormItemRule, value: string) {
                if (value.length === 0) {
                    return new Error("未填写密码");
                }
                return true;
            },
        },
        email: {
            required: true,
            trigger: ["input"],
            message: "请填写邮箱",
            validator(rule: FormItemRule, value: string) {
                const emailReg = /^\w+@[a-z0-9]+(\.[a-z]+){1,3}$/;
                
                if (!emailReg.test(value.trim())) {
                    
					return new Error("邮箱格式错误");
				}
				return true;
			},
        },
        is_admin: {
            required: true,
            trigger: ["input"],
            message: "请选择是否为管理员",
            validator(rule: FormItemRule, value: string) {
                if (value.length === 0) {
                    return new Error("未选择");
                }
                return true;
            },
        },

    },
};


/**
 * 分配角色表单配置
 */
 export const roleFormConfig: Record<string, any> = {
    cols: 24,
    xGap: 24,
    labelPlacement: 'left',
    labelWidth: 120,
    formItems: [
        {
            field: "user_id",
            labelName: "用户id",
            labelStyle: "fontSize:14px;",
            type: "Input",
            inputType: "text",
            inputWidth: "180px",
            disabled:true,
            span: 24
        },
        {
            field: "roles",
            labelName: "角色名称",
            labelStyle: "fontSize:14px;",
            type: "CheckBox",
            span: 24
        },
       

    ],
    rules: {
        
    },
};


